﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Ricebird.Wordprocessing;
using System;
using System.IO;
using System.Linq;

namespace WordOp
{
    class Program
    {
        static void Main()
        {
            using (WordDocument doc = WordDocument.CreateDocument("newDocx.docx", true))
            {
                var titleStyle = doc.CreateStyle("标题样式", def =>
                {
                    def.SetFontFamily("宋体")
                       .SetFontSize(Ricebird.Wordprocessing.FontSize.eh二号)
                       .SetBold()
                       .SetAlign(TextAlign.Center)
                       .SpacingAfter(10)
                       .SetLineSpacing(1.05)
                       .ShowOnUi();
                });

                var normalStyle = titleStyle.CloneStyle("正文样式")
                                            .SetBold(false)
                                            .SetAlign(TextAlign.Left)
                                            .SetFirstLineIndent()
                                            .SetLeftChars(2.23)
                                            .SetFontSize(Ricebird.Wordprocessing.FontSize.sh三号);

                doc.SetDefaultStyle(normalStyle)
                   .AppendParagraph("大学生创新创业训练计划项目", titleStyle)
                   .AppendParagraph("证   明", titleStyle)
                   .AppendParagraph()
                   .AppendParagraph("兹证明某学院教师某某某（教工号：XXXX）指导大创项目如下：")
                   .AppendParagraph("1. 2021年大学生创新创业训练计划项目，项目名称：某个大创项目，项目级别：国家级，项目类别：创新训练，该负责学生李某某，项目执行时间：2020年1月至2021年2月，项目正在进行中。")
                   .AppendParagraph()
                   .AppendParagraph("（以下空白）")
                   .AppendParagraph()
                   .AppendParagraph("教务处")
                        .SetFirstLineIndent(0)
                        .SetAlign(TextAlign.Right)
                   .AppendParagraph("2021年11月10日")
                        .SetFirstLineIndent(0)
                        .SetAlign(TextAlign.Right);

                doc.Save();
            }
        }

        static void Main1(string[] args)
        {
            if (File.Exists("newDocx.docx"))
            {
                File.Delete("newDocx.docx");
            }
            
            using (WordprocessingDocument doc = WordprocessingDocument.Create("newDocx.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
            {
                var main = doc.MainDocumentPart;
                if (doc.MainDocumentPart == null)
                {
                    doc.AddMainDocumentPart();
                }

                if (doc.MainDocumentPart.StyleDefinitionsPart == null)
                {
                    doc.MainDocumentPart.AddNewPart<StyleDefinitionsPart>();
                }
                var sdp = doc.MainDocumentPart.StyleDefinitionsPart;
                if (sdp.Styles == null)
                {
                    sdp.Styles = new Styles();
                }

                Style style = new Style()
                {
                    Type = StyleValues.Paragraph,
                    StyleId = "Normal",
                    Default = new OnOffValue(true),
                    CustomStyle = new OnOffValue(true)
                };
                style.StyleName = new StyleName()
                {
                    Val = "Normal"
                };
                style.StyleParagraphProperties = new StyleParagraphProperties();
                style.StyleParagraphProperties.Append(new ParagraphProperties()
                {
                    SpacingBetweenLines = new SpacingBetweenLines()
                    {
                        After = new StringValue("200"),
                        Line = new StringValue("240")
                    }
                });
                sdp.Styles.Append(style);

                Style style1 = new Style()
                {
                    Type = StyleValues.Paragraph,
                    StyleId = "测试样式名",
                    CustomStyle = new OnOffValue(true)
                };
                style1.StyleName = new StyleName()
                {
                    Val = "测试样式名"
                };
                style1.BasedOn = new BasedOn()
                {
                    Val = "Normal"
                };
                style1.SemiHidden = new SemiHidden()
                {
                    Val = OnOffOnlyValues.Off
                };
                style1.UIPriority = new UIPriority()
                {
                    Val = new Int32Value(99)
                };
                style1.PrimaryStyle = new PrimaryStyle();
                style1.StyleRunProperties = new StyleRunProperties();
                style1.StyleRunProperties.Append(new StyleRunProperties()
                {
                    FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize()
                    {
                        Val = new StringValue("72")
                    }
                });
                sdp.Styles.Append(style1);

                if (doc.MainDocumentPart.Document == null)
                {
                    doc.MainDocumentPart.Document = new Document();
                }
                var body = doc.MainDocumentPart.Document;

                Paragraph para = new Paragraph();
                ParagraphProperties rpp = new ParagraphProperties();
                rpp.ParagraphStyleId = new ParagraphStyleId()
                {
                    Val = "测试样式名"
                };
                
                para.PrependChild(rpp);
                Run r = new Run();
                Text t = new Text();
                t.Text = "Hello World";
                r.Append(t);
                para.Append(r);
                body.Append(para);

                doc.Save();
            }
        }
    }
}